CSharpTest.Net
SynchronizedDictionary<TKey,TValue> Constructor(ILockStrategy)
See Also  Example Send Feedback Download Help File
CSharpTest.Net.Library Assembly > CSharpTest.Net.Collections Namespace > SynchronizedDictionary<TKey,TValue> Class > SynchronizedDictionary<TKey,TValue> Constructor : SynchronizedDictionary<TKey,TValue> Constructor(ILockStrategy)

locking

Glossary Item Box

Constructs a thread-safe generic collection of key/value pairs using the lock provided.

Syntax

Visual Basic (Declaration) 
Public Function New( _
   ByVal locking As ILockStrategy _
)
C# 
public SynchronizedDictionary<TKey,TValue>( 
   ILockStrategy locking
)

Parameters

locking

Example

Library/Library.Test/TestSynchronizedCollections.cs

C#Copy Code
LockCounterFactory<SimpleReadWriteLocking> factory = new LockCounterFactory<SimpleReadWriteLocking>();
ILockStrategy lck = factory.Create();
SynchronizedDictionary<int, string> test = new SynchronizedDictionary<int, string>(lck);

Assert.IsTrue(ReferenceEquals(lck, test.Lock));
test.Add(42, "42");
Assert.AreEqual(1, factory.TotalWriterCount);
Assert.AreEqual(0, factory.TotalReaderCount);

test[42] = "51";
Assert.AreEqual(2, factory.TotalWriterCount);

test.Add(1, "52");
Assert.AreEqual(3, factory.TotalWriterCount);

test.Remove(-1);
Assert.AreEqual(4, factory.TotalWriterCount);

test.Remove(1);
Assert.AreEqual(5, factory.TotalWriterCount);

Assert.AreEqual("51", test[42]);
Assert.AreEqual(1, factory.TotalReaderCount);

foreach (KeyValuePair<int, string> i in test)
    GC.KeepAlive(i);
Assert.AreEqual(2, factory.TotalReaderCount);

Assert.AreEqual(false, test.ContainsKey(-1));
Assert.AreEqual(3, factory.TotalReaderCount);

Assert.AreEqual(1, test.Count);
Assert.AreEqual(4, factory.TotalReaderCount);

string cmp;
Assert.IsTrue(test.TryGetValue(42, out cmp) && cmp == "51");
Assert.AreEqual(5, factory.TotalReaderCount);
VB.NETCopy Code
Dim factory As New LockCounterFactory(Of SimpleReadWriteLocking)()
Dim lck As ILockStrategy = factory.Create()
Dim test As New SynchronizedDictionary(Of Integer, String)(lck)

Assert.IsTrue(ReferenceEquals(lck, test.Lock))
test.Add(42, "42")
Assert.AreEqual(1, factory.TotalWriterCount)
Assert.AreEqual(0, factory.TotalReaderCount)

test(42) = "51"
Assert.AreEqual(2, factory.TotalWriterCount)

test.Add(1, "52")
Assert.AreEqual(3, factory.TotalWriterCount)

test.Remove(-1)
Assert.AreEqual(4, factory.TotalWriterCount)

test.Remove(1)
Assert.AreEqual(5, factory.TotalWriterCount)

Assert.AreEqual("51", test(42))
Assert.AreEqual(1, factory.TotalReaderCount)

For Each i As KeyValuePair(Of Integer, String) In test
    GC.KeepAlive(i)
Next
Assert.AreEqual(2, factory.TotalReaderCount)

Assert.AreEqual(False, test.ContainsKey(-1))
Assert.AreEqual(3, factory.TotalReaderCount)

Assert.AreEqual(1, test.Count)
Assert.AreEqual(4, factory.TotalReaderCount)

Dim cmp As String
Assert.IsTrue(test.TryGetValue(42, cmp) AndAlso cmp = "51")
Assert.AreEqual(5, factory.TotalReaderCount)

Requirements

Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7

See Also

Generated with Document! X 2011 by Innovasys